package com.xiao.androidcompose01.screen.main

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountBox
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.screenModelScope
import cafe.adriel.voyager.koin.koinScreenModel
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.tab.LocalTabNavigator
import cafe.adriel.voyager.navigator.tab.Tab
import cafe.adriel.voyager.navigator.tab.TabOptions
import cafe.adriel.voyager.transitions.FadeTransition
import com.tencent.trtc.TRTCCloud
import com.xiao.androidcompose01.LocalAccountData
import com.xiao.androidcompose01.api.data.AccountData
import com.xiao.androidcompose01.file.AccountFileUtil
import com.xiao.androidcompose01.screen.LoginScreen
import com.xiao.androidcompose01.screen.main.LogoffModel.LogoffState
import com.xiao.androidcompose01.ui.theme.BackgroundColor
import com.xiao.androidcompose01.ui.theme.MainColor
import com.xiao.androidcompose01.weight.MainButton
import com.xiao.libcore.result.withXResult
import io.ktor.client.HttpClient
import io.ktor.client.request.post
import kotlinx.coroutines.launch
import org.koin.core.annotation.Factory
import org.koin.core.annotation.Module
import org.koin.core.context.startKoin


class MineTab : Tab{
   override val options: TabOptions
      @Composable
      get() {
         val title = "我的"
         val icon = rememberVectorPainter(Icons.Default.AccountBox)
         
         return remember {
            TabOptions(
               index = 0u,
               title = title,
               icon = icon
            )
         }
      }
   
   @Composable
   override fun Content() {
      val account = LocalAccountData.current
      val navigator = LocalNavigator.current
      val accountLoginModel = koinScreenModel<LogoffModel>()
      val logoffState by accountLoginModel.state.collectAsStateWithLifecycle(initialValue = null)
      if (logoffState == LogoffState.Success){
         navigator?.parent?.replaceAll(LoginScreen())
         return
      }
      Column(Modifier
         .fillMaxSize()
         .background(BackgroundColor)
         .navigationBarsPadding()
         .statusBarsPadding()) {
         Column(modifier = Modifier
            .fillMaxWidth()
            .padding(top = 20.dp)
            .padding(horizontal = 16.dp)
            .border(1.dp, MainColor, RoundedCornerShape(20.dp))
            .padding(vertical = 12.dp, horizontal = 18.dp)) {
            val roleName: String
            val color: Color
            if (account.isParent()){
               roleName = "房主"
               color = Color.Red
            }else{
               roleName = "成员"
               color = Color.Black
            }
            Text(roleName, color = color, fontSize = 12.sp,modifier = Modifier
               .background(color.copy(0.1f), shape = RoundedCornerShape(30.dp))
               .padding(vertical = 8.dp, horizontal = 6.dp))
            Text(text = account.userName, color = MainColor)
         }

         Spacer(modifier = Modifier.weight(1f))
         MainButton("退出登录", modifier = Modifier
            .fillMaxWidth()
            .padding(bottom = 10.dp)
            .padding(horizontal = 30.dp)) {
            accountLoginModel.logoff()
         }
      }
   }
}

@Module
@Factory
class LogoffModel (
   val httpClient: HttpClient,
   val accountFileUtil: AccountFileUtil,
   private val cloud: TRTCCloud,
) : StateScreenModel<LogoffState>(LogoffState.Init){
   sealed class LogoffState{
      object Init : LogoffState()
      object Loading : LogoffState()
      object Success : LogoffState()
   }
   
   fun logoff(){
      screenModelScope.launch {
         withXResult {
            mutableState.value = LogoffState.Loading
            accountFileUtil.removeAccount()
            cloud.exitRoom()
            httpClient.post("/mobile/logout")
         }.ifSuccess {
            mutableState.value = LogoffState.Success
         }.ifError {
            mutableState.value = LogoffState.Success
         }
      }
   }
}

@Preview
@Composable
private fun MineScreenPreview() {
   startKoin {
      modules()
   }
   val tempAccount = AccountData(
      "","", 0,"","","",
      "","孩子","",arrayListOf("",""),
   )
   CompositionLocalProvider(LocalNavigator provides null,
      LocalAccountData provides tempAccount
   ) {
      MineTab().Content()
   }
}

